var cardInfo = $("#cardIdInfo");
var cardId = $("#cardId");

$(document).ready(function() {
    cardId.blur(validateCardId);

});

function validateCardId() {
    var val = document.getElementById("cardId").value;
    if (val.length < 3) {
        cardInfo.addClass('errorField');
        cardInfo.text("ID must be more than 3 munbers");
    } else if (val.match(/^\d+(?:\.\d+)?$/)) {
        cardInfo.removeClass('errorField');
        cardInfo.text("");
        return true;
    } else {
        cardInfo.addClass('errorField');
        cardInfo.text("Invalid Card ID: " + val);
        return false;
    }
}
function cancel() {
    window.location.href = "?dis/collectM";
}
function collectCard() {
    var params = "cardId=" + document.getElementById("card").value + "&id=" + document.getElementById("id").value + "&returnDate=" + document.getElementById("returnDate").value + "&fromDate=" + document.getElementById("fromDate").value + "&toDate=" + document.getElementById("toDate").value;
    ajaxPost("?dis/updateDistributionCard", params, function(data) {
        var response = JSON.parse(data);
        if (response.status == false) {
            return;
        }
        $("#messagePanel").removeClass();
        $("#messagePanel").addClass(response.type);
        $("#messagePanel").html(response.message);
        enableElement("btnPrint");
        enableElement("btnEdit");

        //initialize();
        // reloadGrid();

    });

}
function load() {
    if (validateCardId()) {
        var params = "cardId=" + document.getElementById("cardId").value;
        ajaxPost("?tld/getCardInfoForCollect", params, function(data) {
            var response = JSON.parse(data);
            if (response.status == false) {
                return;
            }
            $("#cardInfo").html("");
            $("#cardInfo").html(response.tableHtml);
            $("#messagePanel").removeClass();
            $("#messagePanel").html("");

        });

    }
}
function addDatePicker(item) {
    _returnDate = new dhtmlXCalendarObject([item.id]);
    _returnDate.hideTime();
}
function validateDoseValue() {
    var val = document.getElementById("doseValue").value;
    if (val.match(/^\d+(?:\.\d+)?$/)) {
        $("#doseValueInfo").removeClass('errorField');
        $("#doseValueInfo").text("");
        return true;
    } else {

        $("#doseValueInfo").addClass('errorField');
        $("#doseValueInfo").text("Invalid Dose Value: " + val);
        return false;
    }


}
function addDoseValue() {
    if (validateDoseValue()) {
        var params = "id=" + document.getElementById("id").value + "&" + "card=" + document.getElementById("card").value + "&" + encodeURIComponent("doseValue") + "=" + $("#doseValue").val();
        ajaxPost("?dis/updateDoseManually", params, function(data) {
            var response = JSON.parse(data);
            if (response.status == false) {
                return;
            }
            $("#messagePanel").removeClass();
            $("#messagePanel").addClass(response.type);
            $("#messagePanel").html(response.message);
        });

    }
}



